package com.gabry.goodkill
import java.util.UUID

import akka.actor.{ActorSystem, Props}
import com.gabry.goodkill.actor._
import com.gabry.goodkill.protocol.{Purchase, PurchaseFailed}
import com.typesafe.config.ConfigFactory

import scala.util.Random

/**
 * Hello world!
 *
 */
object GoodKill {
  def main(args: Array[String]): Unit = {
    val numberOfGoods = 10*1000
    val numberOfUser = 10*numberOfGoods
    val numberOfPurchasePerUser = 10*10
    val cacheActorNumber = 5
    val activityId = UUID.randomUUID().toString
    val regionActorNumber = 5
    val system = ActorSystem("GoodKill",ConfigFactory.load())
    val stateActor = system.actorOf(Props(new StateActor(numberOfGoods)),s"$activityId-state")
    val cacheActors = 0 until cacheActorNumber map( idx => system.actorOf(Props(new CacheActor(activityId,idx)),s"$activityId-cache-$idx") )
    println(s"stateActor: $stateActor, cacheActors: ${cacheActors.mkString(",")}")

    system.actorOf(Props(new GoodActors(activityId,numberOfGoods)),"goods")

    0 until regionActorNumber foreach { idx =>
      system.actorOf(Props(new RegionActor(numberOfGoods)),s"$activityId-region-$idx")
    }
    Thread.sleep(1000)
    println(s"开始抢购 ${System.currentTimeMillis()}")
    0 until numberOfUser foreach { userId =>
      val regionId = Random.nextInt(regionActorNumber)
      val region = system.actorSelection(s"/user/$activityId-region-$regionId")
      val purchase = Purchase(userId,activityId)
      0 until numberOfPurchasePerUser foreach { _ =>
        region ! purchase
      }
    }
    println( "Hello World!" )
  }
}
